<template>
  <div>
    <div class="search-content">
      <el-form
        ref="searchForm"
        :inline="true"
        :model="searchContent">
        <el-form-item label="兑换时间：" prop="order_time">
          <el-date-picker
            v-model="searchContent.order_time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="兑换单号：" prop="point_ordersn">
          <el-input
            class="search-content-input"
            v-model="searchContent.point_ordersn"
            placeholder="兑换单号">
          </el-input>
        </el-form-item>
        <el-form-item label="会员名称：" prop="point_buyername">
          <el-input
            class="search-content-input"
            v-model="searchContent.point_buyername"
            placeholder="会员名称">
          </el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="member_mobile">
          <el-input
            class="search-content-input"
            v-model="searchContent.member_mobile"
            placeholder="手机号">
          </el-input>
        </el-form-item>
        <el-form-item label="关键字：" prop="point_names" v-if="!externalData">
          <el-input
            class="search-content-input"
            v-model="searchContent.point_names"
            placeholder="关键字">
          </el-input>
        </el-form-item>
        <el-form-item label="供应商：" prop="pgoods_business_txt" v-if="!externalData">
          <el-input
            class="search-content-input"
            v-model="searchContent.pgoods_business_txt"
            placeholder="供应商">
          </el-input>
        </el-form-item>
        <el-form-item label prop="category_id" v-if="!externalData">
          <el-select
            clearable
            v-model="searchContent.category_id"
            placeholder="产品类目">
            <el-option
              v-for="item in category_options"
              :key="item.category_id"
              :label="item.category_name"
              :value="item.category_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="point_paymentcode" v-if="!externalData">
          <el-select
            clearable
            v-model="searchContent.point_paymentcode"
            placeholder="支付方式">
            <el-option label="线下支付" value="offline"></el-option>
            <el-option label="支付宝" value="alipay"></el-option>
            <el-option label="财付通" value="tenpay"></el-option>
            <el-option label="网银在线" value="chinabank"></el-option>
            <el-option label="预存款" value="predeposit"></el-option>
            <el-option label="积分" value="point"></el-option>
            <el-option label="积分+现金" value="point_pay"></el-option>
            <el-option label="其他" value="other"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="point_orderstate" v-if="!externalData">
          <el-select
            clearable
            v-model="searchContent.point_orderstate"
            placeholder="状态">
            <el-option
              v-for="item in type_options"
              :key="item.value_txt"
              :label="item.value_txt"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="payed" v-if="!externalData">
          <el-select
            clearable
            v-model="searchContent.payed"
            placeholder="现金已付">
            <el-option value="0" label="否"></el-option>
            <el-option value="1" label="是"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="payed" v-if="!externalData">
          <el-select
            clearable
            v-model="searchContent.is_bill"
            placeholder="对账状态">
            <el-option value="0" label="未对账"></el-option>
            <el-option value="1" label="已对账"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="结算" v-if="!externalData">
          <el-select
            clearable
            v-model="searchContent.is_settled"
            placeholder="结算状态">
            <el-option value="0" label="未结算"></el-option>
            <el-option value="1" label="已结算"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="exception_state" v-if="!externalData">
          <el-select
            clearable
            v-model="searchContent.exception_state"
            placeholder="异常订单">
            <el-option value="0" label="无异常"></el-option>
            <el-option value="1" label="异常取消"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="is_flash" v-if="!externalData">
          <el-checkbox true-label="1" false-label="0" v-model="searchContent.is_flash">限时抢购</el-checkbox>
        </el-form-item>
        <el-form-item label prop="is_pt" v-if="!externalData">
          <el-checkbox true-label="1" false-label="0" v-model="searchContent.is_pt">拼团订单</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button type="primary" @click="resetSearchData">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="search-list">
      <el-table
        :data="goodsList"
        border
        :height="oHeight"
        :fit="true"
        header-cell-class-name="hdCellStyle" 
        @selection-change="handleSelectionChange">
        <el-table-column
          align="center"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_ordersn"
          label="兑换单号"
          min-width="156"
          #default="{ row }">
          <span :class="`color-${row.exception_state}`">{{row.point_ordersn}}</span>
          <el-tooltip class="item" effect="dark" content="异常取消" placement="right-start" v-if="row.exception_state == 1">
            <i class="el-icon-question color-1" />
          </el-tooltip>
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_names"
          label="商品"
          min-width="200"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_buyername"
          label="会员名称"
          min-width="120">
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_pointnum"
          label="兑换元宝"
          min-width="76">
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_paynum"
          label="现金支付"
          min-width="80">
        </el-table-column>
        <!--el-table-column
          align="center"
          prop="point_shippingfee"
          label="运费"
          min-width="76">
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_paymentname"
          label="支付方式"
          min-width="76">
        </el-table-column-->
        <el-table-column
          align="center"
          prop="point_addtime"
          label="兑换时间"
          min-width="146"
          #default="{ row }">
          {{
            (row.point_addtime * 1000) | dateFormatter("YYYY-MM-DD hh:mm:ss")
          }}
        </el-table-column>
        <el-table-column
          align="center"
          label="支付状态"
          min-width="120"
          #default="{ row }">
          {{ row.point_orderstatetext.order_state }}
          <span v-if="row.point_orderstatetext.change_state">
            {{ `,${row.point_orderstatetext.change_state}` }}
          </span>
        </el-table-column>
        <el-table-column
          align="center"
          prop="is_settled"
          label="结算状态"
          min-width="80"
          #default="{ row }">
          <span v-if="row.is_settled == 0">未结算</span>
          <span v-else>已结算</span>
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_inner"
          label="内部采购"
          min-width="80"
          #default="{ row }">
          <el-checkbox v-model="row.point_inner_checked" @change="handleInnerChange(row)">
          </el-checkbox>
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_origin"
          label="来源"
          min-width="80"
          v-if="!externalData">
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          prop="isInitiative"
          label="操作"
          min-width="100"
          v-if="!externalData"
          #default="{ row }">
          <!-- 订单状态： 
            2:  已取消
            10：已提交，待付款
            11：已付款，待确认
            20：确认收款，待发货
            30：已发货，待收货（显示文字为:确认收货，去评价）
            40：已完成（目前直接跳过评价，进入已完成状态）
            50：未知
            60：已收货，待评价
            65：积分已扣，未付款
            70：已退款
          -->
					<el-dropdown size="mini" trigger="click">
						<el-button type="primary">
  						  展开<i class="el-icon-arrow-down el-icon--right"></i>
  					</el-button>
						<el-dropdown-menu slot="dropdown" class="dropdown-btn">
						    <el-dropdown-item>
                  <el-button type="primary" @click="buttonDeal('showOrder', row)">查看</el-button>
                </el-dropdown-item>
                <!-- 订单状态为：确认收款待发货、已发货待收货、已完成时 -->
                <template v-if="['20', '30', '40'].includes(row.point_orderstate)">
						      <el-dropdown-item v-show="row.point_orderstate == 20">
                    <el-button type="primary" @click="buttonDeal('add', row)">发货</el-button>
                  </el-dropdown-item>
						      <el-dropdown-item v-show="row.point_orderstate == 30">
                    <el-button type="primary" @click="buttonDeal('update', row)">修改物流</el-button>
                  </el-dropdown-item>
                  <!-- 现金支付为 > 0 时 -->
                  <template v-if="parseFloat(row.point_paynum) > 0">
						        <el-dropdown-item>
                      <el-button 
                        :type="getRefundStaus(+row.refund_status).type" 
                        :disabled="getRefundStaus(+row.refund_status).disabled" 
                        @click="refund(row)">
                        {{ getRefundStaus(row.refund_status * 1).text }}
                      </el-button>
                    </el-dropdown-item>
						        <el-dropdown-item v-if="+row.refund_status == 0">
                      <el-button type="danger" @click="refund(row, 2)">标记退款</el-button>
                    </el-dropdown-item>
                  </template>
                  <!-- 现金支付为 0 时 -->
						      <el-dropdown-item v-else>
                    <el-button type="primary" @click="buttonDeal('cancel', row)">
                      取消兑换
                    </el-button>
                  </el-dropdown-item>
						      <el-dropdown-item 
                    v-show="(row.point_orderstate === '30' || row.point_orderstate === '40') && row.is_bill !== '1'">
                    <el-button type="primary" @click="bill(row)">对账</el-button>
                  </el-dropdown-item>
				
                </template>
                <el-dropdown-item v-if="row.point_orderstate == 40 && row.is_bill === '1'">
                    <el-button
                      type="primary"
                      @click="updatesettle(row.point_orderid, row.is_settled * 1, row.refund_status)">
                      {{ getSettled(row.is_settled * 1) }}
                    </el-button>
                  </el-dropdown-item>
						    <el-dropdown-item v-else-if="row.point_orderstate == 65">
                  <el-button type="primary" @click="closeIntegral(row)">关闭</el-button>
                </el-dropdown-item>
						    <el-dropdown-item v-show="row.is_bill === '1' && row.is_review === '0'">
                  <el-button type="primary" @click="billRollback(row)">对账撤回</el-button>
                </el-dropdown-item>
						</el-dropdown-menu>
					</el-dropdown>

          <!-- <el-button type="primary" @click="buttonDeal('showOrder', row)">查看</el-button>
          <template v-if="['20', '30', '40'].includes(row.point_orderstate)"> 
            <el-button
              type="primary"
              v-show="row.point_orderstate == 20"
              @click="buttonDeal('add', row)">
              发货
            </el-button>
            <el-button
              v-show="row.point_orderstate == 30"
              type="primary"
              @click="buttonDeal('update', row)">
              修改物流
            </el-button>
            -- 现金支付为0时
            <template v-if="parseFloat(row.point_paynum) > 0">
              <el-button
                :type="getRefundStaus(row.refund_status * 1).type"
                :disabled="getRefundStaus(row.refund_status * 1).disabled"
                @click="refund(row)">
                {{ getRefundStaus(row.refund_status * 1).text }}
              </el-button>
            </template>
            现金支付>0时
            <template v-else>
              <el-button
                type="primary"
                @click="buttonDeal('cancel', row)"
                >
                取消兑换
              </el-button>
            </template>
            <el-button
              v-show="(row.point_orderstate === '30' || row.point_orderstate === '40') && row.is_bill !== '1'"
              type="primary"
              @click="bill(row)">
              对账
            </el-button>
            针对结算：v-show="row.point_orderstate === '20' || row.point_orderstate === '30' || row.point_orderstate === '40'"
            <el-button
              type="primary"
              @click="updatesettle(row.point_orderid, row.is_settled * 1, row.refund_status)">
              {{ getSettled(row.is_settled * 1) }}
            </el-button>
          </template>
          <template v-else>
            <el-button
              v-if="row.point_orderstate == 65"
              type="primary"
              @click="closeIntegral(row)">
              关闭
            </el-button>
          </template>
          <el-button
            v-show="row.is_bill === '1' && row.is_review === '0'"
            type="primary"
            @click="billRollback(row)">
            对账撤回
          </el-button> -->
          <!--
          <el-button type="primary" @click="showOrder(row)">取消兑换</el-button>
          <el-button type="primary" @click="showOrder(row)">退款</el-button>-->
        </el-table-column>
      </el-table>
      <div class="bottom">
        <div class="btn-group">
          <el-button type="primary" class="btn-bottom" @click="expertData"
            >导出检索数据</el-button
          >
          <el-button type="primary" class="btn-bottom" @click="oneBill" v-if="!externalData"
            >一键对账</el-button
          >
          <el-button type="primary" class="btn-bottom" @click="superBill" v-if="!externalData"
            >一键超级对账</el-button
          >
          
          <el-button type="primary" class="btn-bottom" @click="logistics.dialogImportVisible = true" v-if="!externalData">物流导入</el-button>
          <el-button type="primary" class="btn-bottom" @click="submitExecl" v-if="!externalData"
            >导入结算</el-button
          >
          <!-- <el-button type="primary" class="btn-bottom" @click="oneBill(1)">一键撤回</el-button> -->
        </div>
        <!--分页部分-->
        <div class="paginationOne">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchContent.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="searchContent.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </div>
    <order-info ref="orderInfo"></order-info>
    <update-drawer ref="updateDrawer"></update-drawer>

    <!--文件-->
    <el-dialog
      title="提交结算"
      :visible.sync="dialogFormVisible"
      class="admin-control-edit"
    >
      <el-upload
        style="width: 200px"
        class="upload-demo"
        action="#"
        :auto-upload="false"
        :limit="1"
        :on-change="
          (val, list) => {
            codeChange(val, list);
          }
        "
        :file-list="codeFileList"
      >
        <el-button size="primary" type="primary">选择文件</el-button>
      </el-upload>
      <el-link
        class="tpyeDown"
        target="_blank"
        :href='$store.state.location.origin + "/admin/adminImports/index.php?act=pointOrderSettleImport&op=template"'
        >模板下载</el-link
      >
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="物流导入" :visible.sync="logistics.dialogImportVisible" class="logistics">
      <el-form ref="importForm" :rules="logistics.rules" :model="logistics" class="logisticsForm">
        <el-form-item prop="fileList">
          <el-row>
            <el-col :span="12">
              <el-upload
                action="#"
                :auto-upload="false"
                :on-change="handleLogisticsChange"
                :multiple="false"
                :limit="1"
                :on-remove="handleLogisticsRemove"
                :accept="logistics.imageAccept.join(',')"
                :file-list="logistics.fileList"
                class="importUpload"
                ref="fileUpload"
              >
                <el-button size="small" type="primary">添加文件</el-button>
                <div slot="tip" class="el-upload__tip" style="padding-top:1px;"><a href="https://4s.chejj.cn/download/物流-导入模板.xls" style="color:#409EFF;font-size:14px;">模板下载</a></div>
              </el-upload>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <div class="el-upload__tip">只能上传{{logistics.imageAccept.join(',')}}文件，且不超过50M</div>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item style="text-align:right;margin-bottom:0px;">
          <el-button size="small" @click="cancelLogistics">取消导入</el-button>
          <el-button type="primary" size="small" :disabled="logistics.fileList.length > 0 ? false : true" @click="sendLogistics">点击提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { resize } from "@/common/js/height.js";
import orderInfo from "./order-info-drawer";
import updateDrawer from "./update-shipInfo-drawer";
import { dateFormatter, objScreen } from "@/common/js/util";
export default {
	props: {
    externalData: { // 注意此属性不要删除，当此组件被单独引入第三方组件时候的父组件传值属性
			type: Object,
			default: null
		} 
	},
  components: { orderInfo, updateDrawer },
  data() {
    return {
      goodsList: [],
      oHeight: 800,
      searchContent: {
        currentPage: 1,
        pageSize: localStorage.getItem("pageSize") * 1 || 20,
        order_time: "",
        point_ordersn: "",
        point_buyername: "",
        member_mobile: "",
        point_names: "",
        pgoods_business_txt: "",
        category_id: "",
        point_paymentcode: "",
        point_orderstate: "",
        payed: "",
        is_bill: "",
        is_settled: "",
        originate: '',
        is_flash: '',
        is_pt: '',
        is_valid: '',
        exception_state: ''
      },
      originate: '',
      dialogFormVisible: false,
      codeFileList: [],
      searchObj: {},
      total: 0,
      category_options: [],
      type_options: [],
      pgoods_brand_options: [],
      check: [],
      execlErr: [],
      logistics: {
        dialogImportVisible: false,
        fileList: [],
        imageAccept: ['.xlsx', '.xls'],
        rules: {
          fileList: [
              { required: true, message: '请添加导入文件', trigger: 'change' }
          ],
        }
      }
    };
  },
  created() {
		if(this.externalData) {
				// this.searchContent.originate = this.externalData.originate;
				// this.searchObj.originate = this.externalData.originate;
        this.searchContent.is_valid = this.externalData.is_valid
				this.searchObj.is_valid = this.externalData.is_valid;
		}
    this.getListData();
    this.beforeData();
  },
  methods: {
    // 更新采购商品状态
    handleInnerChange(row) {
      const datas = {
        order_id: row.point_orderid,
        point_inner: row.point_inner_checked ? 1 : 0
      }
      this.$http.post("/admin/pointprod/index.php?act=pointOrder&op=update", datas)
      .then(res => {
        if (res.code == 1) {
          this.$message.success(`${res.msg}`)
        } else {
          row.point_inner_checked = false
          this.$message.error(`${res.msg}`)
        }
      })
    },
    handleLogisticsChange(file, fileList) {
      const fileName = file.name.substring(file.name.lastIndexOf('.'));
      const isValid = this.logistics.imageAccept.indexOf(fileName);
      const isLt50M = file.size / 1024 / 1024 < 50;
      if (isValid < 0) {
        this.$message.error('文件类型限制为：'+this.logistics.imageAccept.join(", ")+' 格式!');
      }
      if (!isLt50M) {
          this.$message.error('上传文件大小不能超过 50MB!');
      }
      if(isValid < 0 || !isLt50M) {
        this.logistics.fileList = this.logistics.fileList.filter(item => file.name !== item.name);
        return false;
      }
      this.logistics.fileList = fileList;
      this.$refs.importForm.clearValidate("fileList"); // 移除fileList校验结果
    },
    handleLogisticsRemove(files, fileList) {
      this.logistics.fileList = fileList;
    },
    sendLogistics() {
      this.$refs.importForm.validate(valid => {
        if(valid) {
          let datas = new FormData();
          datas.append("file", this.logistics.fileList[0].raw);
          this.$http.post("/admin/pointprod/index.php?act=pointOrder&op=updateShipInfoBatch", datas).then(res => {console.log(res)
            if(res.code == 1) {
              //this.$refs.fileUpload.clearFiles(); // 清除所有添加的文件
              //this.logistics.fileList = [];
              this.$refs.importForm.resetFields(); // 重置
              this.$message.success("导入成功");
              this.logistics.dialogImportVisible = false;
              
              // 判断是否有导入失败的订单
              if(res.data && res.data.length > 0) {
                this.$alert(`<div>${res.data.join("<br>")}</div>`, '导入失败订单号', {
                  confirmButtonText: '确定',
                  dangerouslyUseHTMLString: true,
                  callback: () => {
                    this.getListData();
                  }
                });
              }
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          this.$message.error("请上传导入文件");
        }
      })
    },
    cancelLogistics() {
      this.logistics.dialogImportVisible = false;
      this.$refs.importForm.resetFields();
    },
    beforeData() {
      var data = {
        page: 1,
        pageSize: 99999,
      };
      this.$http
        .get(
          `/admin/pointprod/index.php?act=pointCategory&op=list&parent_id=0`,
          {
            params: data,
          }
        )
        .then((res) => {
          this.category_options = res.data.list;
        })
        .catch((err) => {
          this.$message.error(err);
        });
      this.$http
        .get(`/admin/pointprod/index.php?act=pointOrder&op=stateList`, {
          params: data,
        })
        .then((res) => {
          this.type_options = res.data;
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
      closeIntegral(item) {
      let that = this;
      this.$confirm("确认关闭？", '温馨提示', {type: 'warning'}).then((res) => {
        this.$http.post("/admin/pointprod/index.php?act=pointOrder&op=closePointOrder", {
          pay_ordersn: item.point_ordersn,
        }).then((res) => {
          if (res.code == 1) {
             this.$message.success(res.msg);
            that.getListData(); //刷新列表
          } else {
            alert(res.msg);
          }
        });
      }).catch((res) => {
        console.log(res);
      });
    },
    refund(item, type = 1) {
      let [
        content, 
        url,
        datas
      ] = [
        '退款', 
        '/admin/bill/index.php?act=commonRefund&op=refund',
        {pay_ordersn: item.point_ordersn}
      ]
      if (type === 2) {
        content = '标记退款'
        url = '/admin/pointprod/index.php?act=pointOrder&op=refundTag'
        datas = {point_orderid: item.point_orderid}
      }
      this.$confirm(
        '确认' + content + "？", 
        '温馨提示', 
        { type: 'warning' }
      ).then(() => {
        this.$http.post(url, datas).then(res => {
          if (res.code == 1) {
            this.$message.success(`${content}成功`)
            this.getListData()
          } else {
            res.msg && this.$message.error(`${res.msg}`)
          }
        }).catch(error => {
          error && this.$message.error(`${error}`)
        })
      }).catch()
    },
    // 点击编辑
    submitExecl() {
      this.dialogFormVisible = true;
    },
    codeChange(val, list) {
      this.codeFileList = list;
    },
    oneBill(type) {
      let url = "/admin/bill/index.php?act=onlineBill&op=bill";
      let idArr = [];
      let dataErr = false;
      if (this.check.length === 0) {
        this.$message.warning("请勾选数据");
        return;
      }
      this.check.forEach((item) => {
        if (
          (item.point_orderstate === "30" || item.point_orderstate === "40") &&
          item.is_bill !== "1"
        ) {
          idArr.push(item.point_orderid);
        } else {
          dataErr = true;
        }
      });
      if (dataErr) {
        this.$message.error("请选择正确的状态！");
        return;
      }
      if (type === 1) {
        url = "/admin/bill/index.php?act=onlineBill&op=billRollback";
      }
      this.$http
        .post(url, {
          id: idArr,
          order_type: "1",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.search();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    superBill() {
      if (this.total > 10000) {
        return this.$message.error('数据超10000条，请先筛选！')
      }
      var data = this.searchObj;
      data.order_type = 1;
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=superBill", data)
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.search();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    handleSelectionChange(val) {
      this.check = val;
    },
    //上传excel文件
    submit() {
      console.log(this.codeFileList[0].raw);
      if (this.codeFileList.length) {
        let data = new FormData();
        data.append("file", this.codeFileList[0].raw);
        this.$http
          .post(
            "/admin/adminImports/index.php?act=pointOrderSettleImport&op=run",
            data
          )
          .then((res) => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.dialogFormVisible = false;
              this.execlErr = res.data;
              if (this.execlErr.length > 0) {
                this.$alert(this.execlErr, "错误单号", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            } else {
              this.$message.error(res.msg);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      } else {
        alert("请选择文件");
      }
    },
    getListData() {
      var data = this.searchObj;
      data.page = this.searchContent.currentPage;
      data.pageSize = this.searchContent.pageSize;
      this.$http
        .get("/admin/pointprod/index.php?act=pointOrder&op=list", {
          params: data,
        })
        .then((res) => {
          if (res.code === 1) {
            this.total = res.data.totalCount;
            this.goodsList = res.data.list.map(item => {
              if(item.point_inner == 1) item.point_inner_checked = true
              else item.point_inner_checked = false
              return item
            })
            // this.$message.success( res.msg);
            // this.getListData();
          } else {
            this.total = 0;
            this.goodsList = [];
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    search() {
      this.searchContent.currentPage = 1;
      let order_time = this.searchContent.order_time;
      this.searchObj = objScreen({
        order_start_time: order_time[0] && parseInt(order_time[0].getTime() / 1000),
        order_end_time: order_time[1] && parseInt((order_time[1].getTime() / 1000+(24 * 60 * 60 - 1))),
        point_ordersn: this.searchContent.point_ordersn,
        point_buyername: this.searchContent.point_buyername,
        pgoods_name: this.searchContent.pgoods_name,
        pgoods_show: this.searchContent.pgoods_show,
        category_id: this.searchContent.category_id,
        pgoods_ifpay: this.searchContent.pgoods_ifpay,
        pgoods_brand: this.searchContent.pgoods_brand,
        pgoods_business_txt: this.searchContent.pgoods_business_txt,
        pgoods_travel: this.searchContent.pgoods_travel,
        pgoods_virtual: this.searchContent.pgoods_virtual,
        pgoods_serial: this.searchContent.pgoods_serial,
        point_inner: this.searchContent.point_inner,
        point_names: this.searchContent.point_names,
        member_mobile: this.searchContent.member_mobile,
        point_paymentcode: this.searchContent.point_paymentcode,
        point_orderstate: this.searchContent.point_orderstate,
        payed: this.searchContent.payed,
        is_bill: this.searchContent.is_bill,
        is_settled: this.searchContent.is_settled,
        originate: this.searchContent.originate,
        is_valid: this.searchContent.is_valid,
        is_flash: this.searchContent.is_flash,
        is_pt: this.searchContent.is_pt,
        exception_state: this.searchContent.exception_state
      });
      this.getListData();
    },
    getRefundStaus(status) {
      let type = "info";
      let text = "退款";
      let disabled = true;
      switch (status) {
        case 0:
          type = "primary";
          text = "退款";
          disabled = false;
          break;
        case 10:
          type = "info";
          text = "退款中";
          disabled = true;
          break;
        case 20:
          type = "info";
          text = "已退款";
          disabled = true;
          break;
        case 30:
          type = "info";
          text = "退款失败";
          disabled = true;
          break;
      }
      let a = {
        type,
        text,
        disabled,
      };
      return a;
    },

    getSettled(status) {
      let text = "";
      if (status == "0") {
        text = "供应商结算";
      } else {
        text = "供应商结算撤回";
      }
      return text;
    },
    updatesettle(id, status, refund) {
      let settled;
      if (status == 0) {
        if (refund == "10") {
          if (confirm("订单正在退款中，确实要结算吗？")) {
            settled = "1";
            this.jiesuanApi(id, settled);
          }
        } else {
          if (confirm("确定要结算吗？")) {
            settled = "1";
            this.jiesuanApi(id, settled);
          }
        }
      } else {
        this.$confirm(
          "确实要撤回结算吗？", 
          '温馨提示', 
          { type: 'warning' }
        ).then((res) => {
          settled = "0";
          this.jiesuanApi(id, settled);
        }).catch((res) => {
          console.log(res)
        });
      }
    },
    jiesuanApi(id, settled) {
      this.$http
        .post(
          "/admin/pointprod/index.php?act=pointOrder&op=switchOrderSettleStatus",
          {
            point_orderid: id,
            settle_type: settled,
          }
        )
        .then((res) => {
          if (res.code == 0) {
            this.$message.success("修改成功！");
          } else {
            this.$message.error(res.msg);
          }
          this.getListData();
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    resetSearchData() {
      // console.log(this.$refs);
      this.$refs["searchForm"].resetFields();
      this.searchContent.is_bill = "";
      this.searchContent.is_settled = "";
      this.search();
    },
    expertData() {
      var host;
      if (this.$store.state.isServer) {
        host = location.host.replace("admin", "api");
      } else {
        host = location.host + "/api";
      }
      var url =
        location.protocol +
        "//" +
        host +
        "/admin/pointprod/index.php?act=pointOrder&op=list&export=1";
      var searchData = this.searchObj;
      for (var key in searchData) {
        if (
          !(
            searchData[key] === null ||
            searchData[key] === "" ||
            searchData[key] === undefined ||
            key === "page" ||
            key === "pageSize" ||
            key === "currentPage"
          )
        ) {
          url += "&" + key + "=" + searchData[key];
        }
      }
      // console.log(url);
      window.location.href = url;
    },
    handleSizeChange(pageSize) {
      localStorage.setItem("pageSize", pageSize);
      this.searchContent.pageSize = pageSize;
      this.searchContent.currentPage = 1;
      this.getListData();
    },
    handleCurrentChange(page) {
      this.searchContent.currentPage = page;
      this.getListData();
    },
    buttonDeal(type, row) {
      switch (type) {
        case "showOrder":
          this.$refs.orderInfo.show_dialog_fn(row);
          break;
        case "add":
          this.$refs.updateDrawer.show_dialog_fn(row, 1);
          break;
        case "update":
          this.$refs.updateDrawer.show_dialog_fn(row);
          break;
        case "cancel":
          this.$confirm(
            "确认取消兑换？", 
            '温馨提示', 
            { type: 'warning' }
          ).then((res) => {
            this.cancel(row)
          }).catch((res) => {
            console.log(res)
          });
          break;
      }
    },
    cancel(row) {
      this.$http
        .post("/admin/pointprod/index.php?act=pointOrder&op=cancel", {
          orderId: row.point_orderid,
        })
        .then((res) => {
          if (res.code === 1) {
            row.point_orderstate = 2;
            this.$message.success(res.msg);
            this.getListData();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    bill(row) {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=bill", {
          id: row.point_orderid,
          order_type: "1",
        })
        .then((res) => {
          if (res.code === 1) {
            row.is_bill = "1";
            this.$message.success(res.msg);
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    billRollback(row) {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=billRollback", {
          id: row.point_orderid,
          order_type: "1",
        })
        .then((res) => {
          if (res.code === 1) {
            row.is_bill = "0";
            this.$message.success(res.msg);
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
  },
  mounted() {
    resize(this);
  },
};
</script>
<style lang="scss" scoped>
.color-1 {
  color: #ff3300;
}
.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.search-content-input {
  width: 200px;
}
.tpyeDown {
  color: #409eff;
  margin-top: 10px;
}
.logistics {
    /deep/.el-dialog {
      max-width: 680px;
    }
    .logisticsForm /deep/.el-upload--text{
      float: left;
      margin-right: 10px;
    }
}
	.dropdown-btn {
    padding: 6px 0;
		.el-dropdown-menu__item {
			padding: 3px 10px;
			&:hover, &:focus, &:active {
				background-color: #fff;
			}
			.el-button {
				width: 108px;
        padding: 7px 10px;
			}
		}
	}
</style>
